package middleware

import (
	"cs-backend/util"
	"github.com/iris-contrib/middleware/jwt"
	"github.com/kataras/iris/v12"
)

func Before(ctx iris.Context) {
	println("Before Middleware")

	j := ctx.Values().Get("jwt").(*jwt.Token)

	foobar := j.Claims.(jwt.MapClaims)
	var username string
	var userid string
	for key, value := range foobar {
		if key == "username" {
			username = value.(string)
		}
		if key == "userid" {
			userid = value.(string)
		}

	}
	ctx.Values().Set("username", username)
	ctx.Values().Set("userid", userid)
	token := util.GenerateToken(username, userid)
	ctx.ResponseWriter().Header().Add("token", token)
	ctx.Next()
}
func After(ctx iris.Context) {
	println("After Middleware")
}
